<?php

namespace app\admin\model\wechat;

use think\Model;


class Fans extends Model
{


    // 表名
    protected $name = 'wechat_fans';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
//        'subscribe_time_text',
//        'unsubscribe_time_text'
    ];

    public function third(){
        return $this->belongsTo('app\\admin\\model\\Third', 'unionid', 'unionid', [], 'LEFT')->field('unionid,user_id');
    }

    public function getSubscribeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['subscribe_time']) ? $data['subscribe_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUnsubscribeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['unsubscribe_time']) ? $data['unsubscribe_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setSubscribeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUnsubscribeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
